package com.yutel.phone.xplayer.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.yutel.phone.xplayer.R;
import com.yutel.phone.xplayer.service.AikaServer;
import com.yutel.silver.Aika.AikaControlListener;
import com.yutel.silver.exception.AirplayException;
import com.yutel.silver.vo.AirplayState;

public class PlayerActivity extends Activity implements AikaControlListener,
		MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener,
		MediaPlayer.OnPreparedListener {
	private static final String TAG = "PlayerActivity";
	private VideoView mVideoView;
	private MediaController mMediaController;
	private Uri mUri;
	private int mPositionWhenPaused = 0;
	private int state = AirplayState.CACHING;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.player_layout);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		mVideoView = (VideoView) findViewById(R.id.videoview);
		// Create media controller
		mMediaController = new MediaController(this);
		// 设置MediaController
		mVideoView.setMediaController(mMediaController);
		mVideoView.setOnPreparedListener(this);
		mVideoView.setOnCompletionListener(this);
		mVideoView.setOnErrorListener(this);
		AikaServer.getInstance().getAika().setControlListener(this);
	}

	// 开始
	@Override
	public void onStart() {
		String url = getIntent().getStringExtra(Intent.EXTRA_TEXT);
		mUri = Uri.parse(url);
		// Play Video
		mVideoView.setVideoURI(mUri);
		super.onStart();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		String url = getIntent().getStringExtra(Intent.EXTRA_TEXT);
		mUri = Uri.parse(url);
		// Play Video
		mVideoView.setVideoURI(mUri);
	}

	// 暂停
	@Override
	public void onPause() {
		// Stop video when the activity is pause.
		mPositionWhenPaused = mVideoView.getCurrentPosition();
		mVideoView.stopPlayback();
		Log.d(TAG, "OnStop: mPositionWhenPaused = " + mPositionWhenPaused);
		Log.d(TAG, "OnStop: getDuration = " + mVideoView.getDuration());
		super.onPause();
	}

	@Override
	public void onResume() {
		// Resume video player
		if (mPositionWhenPaused > 0) {
			mVideoView.seekTo(mPositionWhenPaused);
			mPositionWhenPaused = 0;
		}
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (mVideoView.isPlaying()) {
			mVideoView.pause();
			mVideoView.destroyDrawingCache();
		}
		super.onDestroy();
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		Log.i(TAG, "onCompletion");
		finish();
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		Log.i(TAG, "onError");
		Toast.makeText(this, "play error!", Toast.LENGTH_LONG).show();
		finish();
		return false;
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		float posf = getIntent().getFloatExtra("position", 0f);
		int dura = mp.getDuration();
		int seek = (int) (dura * posf);
		mVideoView.seekTo(seek);
		mVideoView.start();
	}

	@Override
	public void videoPause() throws AirplayException {
		mVideoView.pause();
	}

	@Override
	public void videoStop() throws AirplayException {
		mVideoView.pause();
		finish();
	}

	@Override
	public void videoSeek(int pos) throws AirplayException {
		state = AirplayState.SEEKING;
		mVideoView.seekTo(pos * 1000);
		mVideoView.start();
		state = AirplayState.PLAYING;
	}

	@Override
	public void videoResume() throws AirplayException {
		mVideoView.start();
	}

	@Override
	public int videoStatus() {
		return state;
	}

	@Override
	public int videoPostion() {
		try {
			return mVideoView.getCurrentPosition() / 1000;
		} catch (Exception e) {
			return AirplayState.ERROR;
		}
	}

	@Override
	public int videoDuration() {
		try {
			return mVideoView.getDuration() / 1000;
		} catch (Exception e) {
			return AirplayState.ERROR;
		}
	}

}
